package dobest.ray.com.jiongjiong.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dobest.ray.corelibs.utils.ToastMgr;
import com.dobest.ray.corelibs.utils.Tools;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by Ray on 2016/3/16.
 * https://github.com/ray0807
 */
public class RegisterSetPasswordActivity extends BaseActivity<LoginPresenter> implements View.OnClickListener, PresenterCallBack {
    private Button btn_complete;
    private static String phoneTag = "phoneTag";
    private static String codeTag = "codeTag";
    private static String isRegisterTag = "isRegister";

    private EditText edit_password_repeat;
    private EditText edit_password;
    private String password;
    private String passwordRepeat;

    private String phone;
    private String code;
    private boolean isRegister;


    public static void startSetPassword(Context context, String phone, String code, boolean isRegister) {
        Intent intent = new Intent(context, RegisterSetPasswordActivity.class);
        intent.putExtra(isRegisterTag, isRegister);
        intent.putExtra(phoneTag, phone);
        intent.putExtra(codeTag, code);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_set_pwd);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        phone = getIntent().getStringExtra(phoneTag);
        code = getIntent().getStringExtra(codeTag);
        isRegister = getIntent().getBooleanExtra(isRegisterTag, true);

        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setTitle("设置密码");
        navi.setBackListener(R.mipmap.img_back, "返回");
        btn_complete = (Button) findViewById(R.id.btn_complete);
        edit_password = (EditText) findViewById(R.id.edit_password);
        edit_password_repeat = (EditText) findViewById(R.id.edit_password_repeat);

    }

    @Override
    public void init() {
        setPresenter(new LoginPresenter(this), this);
    }

    @Override
    public void addListeners() {
        btn_complete.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_complete:
                password = edit_password.getText().toString().trim();
                passwordRepeat = edit_password_repeat.getText().toString().trim();
                if (Tools.isNull(password) || Tools.isNull(password)) {
                    ToastMgr.show(getResources().getString(R.string.notice_str_is_null));
                    return;
                }
                if (!password.equals(passwordRepeat)) {
                    ToastMgr.show(getResources().getString(R.string.notice_password_is_not_equal));
                    return;
                }
                if (!Tools.validateLoginPassWord(password)) {
                    ToastMgr.show(getResources().getString(R.string.notice_password_is_not_equal));
                    return;
                }
                getPresenter().doRegister(this, phone, password, code);
                showToast("完成");
                break;
        }
    }

    @Override
    public <T> void callBackPresenter(PresenterData<T> data) {

    }
}
